/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hello;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Random;
import org.netbeans.microedition.lcdui.SplashScreen;

/**
 * @author Administrador
 */
public class HelloMIDlet extends MIDlet implements CommandListener {

    private boolean midletPaused = false;

    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Command exitCommand2;
    private Command backCommand;
    private Command okCommand;
    private List menuPrincipal;
    private Form telaRolagem;
    private TextField textField1;
    private TextField textField;
    private Spacer spacer1;
    private TextField textField2;
    private SplashScreen splashScreen;
    private Font font1;
    private Image image1;
    //</editor-fold>//GEN-END:|fields|0|

    /**
     * The HelloMIDlet constructor.
     */
    public HelloMIDlet() {
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getSplashScreen());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == menuPrincipal) {//GEN-BEGIN:|7-commandAction|1|68-preAction
            if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|1|68-preAction
                // write pre-action user code here
                menuPrincipalAction();//GEN-LINE:|7-commandAction|2|68-postAction
                // write post-action user code here
            } else if (command == exitCommand2) {//GEN-LINE:|7-commandAction|3|78-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|4|78-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|5|91-preAction
        } else if (displayable == splashScreen) {
            if (command == SplashScreen.DISMISS_COMMAND) {//GEN-END:|7-commandAction|5|91-preAction
                // write pre-action user code here
                switchDisplayable(null, getMenuPrincipal());//GEN-LINE:|7-commandAction|6|91-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|7|82-preAction
        } else if (displayable == telaRolagem) {
            if (command == backCommand) {//GEN-END:|7-commandAction|7|82-preAction
                // write pre-action user code here
                switchDisplayable(null, getMenuPrincipal());//GEN-LINE:|7-commandAction|8|82-postAction
                // write post-action user code here
            } else if (command == okCommand) {//GEN-LINE:|7-commandAction|9|30-preAction
                // write pre-action user code here
//GEN-LINE:|7-commandAction|10|30-postAction
                // write post-action user code here
            try{
            int numeroDados = Integer.parseInt(textField.getString());
            int roladaTotal = 0;
            int roladaAtual = 0;
            String resultado = "";
            Random aleatorio = new Random();
            //String roladas = "   ";
            for(int i = 0; i< numeroDados;i++){
                roladaAtual = (int)(aleatorio.nextDouble()*(Integer.parseInt(textField1.getString()))+1);
                resultado += roladaAtual + " ";
                roladaTotal += roladaAtual;
            }
           textField2.setString(roladaTotal+" ( "+resultado+")");

           }catch(Exception exc){


           }
            }//GEN-BEGIN:|7-commandAction|11|7-postCommandAction
        }//GEN-END:|7-commandAction|11|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|12|
    //</editor-fold>//GEN-END:|7-commandAction|12|







    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: telaRolagem ">//GEN-BEGIN:|14-getter|0|14-preInit
    /**
     * Returns an initiliazed instance of telaRolagem component.
     * @return the initialized component instance
     */
    public Form getTelaRolagem() {
        if (telaRolagem == null) {//GEN-END:|14-getter|0|14-preInit
            // write pre-init user code here
            telaRolagem = new Form("Rolador", new Item[] { getTextField(), getTextField1(), getSpacer1(), getTextField2() });//GEN-BEGIN:|14-getter|1|14-postInit
            telaRolagem.addCommand(getOkCommand());
            telaRolagem.addCommand(getBackCommand());
            telaRolagem.setCommandListener(this);//GEN-END:|14-getter|1|14-postInit
            // write post-init user code here
        }//GEN-BEGIN:|14-getter|2|
        return telaRolagem;
    }
    //</editor-fold>//GEN-END:|14-getter|2|



    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: textField ">//GEN-BEGIN:|24-getter|0|24-preInit
    /**
     * Returns an initiliazed instance of textField component.
     * @return the initialized component instance
     */
    public TextField getTextField() {
        if (textField == null) {//GEN-END:|24-getter|0|24-preInit
            // write pre-init user code here
            textField = new TextField("Numero de Dados", null, 32, TextField.NUMERIC);//GEN-LINE:|24-getter|1|24-postInit
            // write post-init user code here
        }//GEN-BEGIN:|24-getter|2|
        return textField;
    }
    //</editor-fold>//GEN-END:|24-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: textField1 ">//GEN-BEGIN:|25-getter|0|25-preInit
    /**
     * Returns an initiliazed instance of textField1 component.
     * @return the initialized component instance
     */
    public TextField getTextField1() {
        if (textField1 == null) {//GEN-END:|25-getter|0|25-preInit
            // write pre-init user code here
            textField1 = new TextField("Numero de Faces", null, 32, TextField.NUMERIC);//GEN-LINE:|25-getter|1|25-postInit
            // write post-init user code here
        }//GEN-BEGIN:|25-getter|2|
        return textField1;
    }
    //</editor-fold>//GEN-END:|25-getter|2|



    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: spacer1 ">//GEN-BEGIN:|27-getter|0|27-preInit
    /**
     * Returns an initiliazed instance of spacer1 component.
     * @return the initialized component instance
     */
    public Spacer getSpacer1() {
        if (spacer1 == null) {//GEN-END:|27-getter|0|27-preInit
            // write pre-init user code here
            spacer1 = new Spacer(16, 1);//GEN-BEGIN:|27-getter|1|27-postInit
            spacer1.setPreferredSize(-1, 20);//GEN-END:|27-getter|1|27-postInit
            // write post-init user code here
        }//GEN-BEGIN:|27-getter|2|
        return spacer1;
    }
    //</editor-fold>//GEN-END:|27-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: textField2 ">//GEN-BEGIN:|28-getter|0|28-preInit
    /**
     * Returns an initiliazed instance of textField2 component.
     * @return the initialized component instance
     */
    public TextField getTextField2() {
        if (textField2 == null) {//GEN-END:|28-getter|0|28-preInit
            // write pre-init user code here
            textField2 = new TextField("Resultado", null, 200, TextField.ANY | TextField.UNEDITABLE);//GEN-LINE:|28-getter|1|28-postInit
            // write post-init user code here
        }//GEN-BEGIN:|28-getter|2|
        return textField2;
    }
    //</editor-fold>//GEN-END:|28-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">//GEN-BEGIN:|29-getter|0|29-preInit
    /**
     * Returns an initiliazed instance of okCommand component.
     * @return the initialized component instance
     */
    public Command getOkCommand() {
        if (okCommand == null) {//GEN-END:|29-getter|0|29-preInit
            // write pre-init user code here
            okCommand = new Command("Ok", Command.OK, 0);//GEN-LINE:|29-getter|1|29-postInit
            // write post-init user code here
        }//GEN-BEGIN:|29-getter|2|
        return okCommand;
    }
    //</editor-fold>//GEN-END:|29-getter|2|













    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: image1 ">//GEN-BEGIN:|57-getter|0|57-preInit
    /**
     * Returns an initiliazed instance of image1 component.
     * @return the initialized component instance
     */
    public Image getImage1() {
        if (image1 == null) {//GEN-END:|57-getter|0|57-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|57-getter|1|57-@java.io.IOException
                image1 = Image.createImage("/hello/Witch Hat.png");
            } catch (java.io.IOException e) {//GEN-END:|57-getter|1|57-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|57-getter|2|57-postInit
            // write post-init user code here
        }//GEN-BEGIN:|57-getter|3|
        return image1;
    }
    //</editor-fold>//GEN-END:|57-getter|3|



    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: menuPrincipal ">//GEN-BEGIN:|67-getter|0|67-preInit
    /**
     * Returns an initiliazed instance of menuPrincipal component.
     * @return the initialized component instance
     */
    public List getMenuPrincipal() {
        if (menuPrincipal == null) {//GEN-END:|67-getter|0|67-preInit
            // write pre-init user code here
            menuPrincipal = new List("Menu Principal", Choice.IMPLICIT);//GEN-BEGIN:|67-getter|1|67-postInit
            menuPrincipal.append("Rolador", null);
            menuPrincipal.append("Controlador de PVs", null);
            menuPrincipal.append("Op\u00E7\u00F5es", null);
            menuPrincipal.append("Sair", null);
            menuPrincipal.addCommand(getExitCommand2());
            menuPrincipal.setCommandListener(this);
            menuPrincipal.setFitPolicy(Choice.TEXT_WRAP_DEFAULT);
            menuPrincipal.setSelectedFlags(new boolean[] { false, false, false, false });//GEN-END:|67-getter|1|67-postInit
            // write post-init user code here
        }//GEN-BEGIN:|67-getter|2|
        return menuPrincipal;
    }
    //</editor-fold>//GEN-END:|67-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: menuPrincipalAction ">//GEN-BEGIN:|67-action|0|67-preAction
    /**
     * Performs an action assigned to the selected list element in the menuPrincipal component.
     */
    public void menuPrincipalAction() {//GEN-END:|67-action|0|67-preAction
        // enter pre-action user code here
        String __selectedString = getMenuPrincipal().getString(getMenuPrincipal().getSelectedIndex());//GEN-BEGIN:|67-action|1|70-preAction
        if (__selectedString != null) {
            if (__selectedString.equals("Rolador")) {//GEN-END:|67-action|1|70-preAction
                // write pre-action user code here
                switchDisplayable(null, getTelaRolagem());//GEN-LINE:|67-action|2|70-postAction
                // write post-action user code here
            } else if (__selectedString.equals("Controlador de PVs")) {//GEN-LINE:|67-action|3|94-preAction
                // write pre-action user code here
//GEN-LINE:|67-action|4|94-postAction
                // write post-action user code here
            } else if (__selectedString.equals("Op\u00E7\u00F5es")) {//GEN-LINE:|67-action|5|71-preAction
                // write pre-action user code here
//GEN-LINE:|67-action|6|71-postAction
                // write post-action user code here
            } else if (__selectedString.equals("Sair")) {//GEN-LINE:|67-action|7|72-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|67-action|8|72-postAction
                // write post-action user code here
            }//GEN-BEGIN:|67-action|9|67-postAction
        }//GEN-END:|67-action|9|67-postAction
        // enter post-action user code here
    }//GEN-BEGIN:|67-action|10|
    //</editor-fold>//GEN-END:|67-action|10|



    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand2 ">//GEN-BEGIN:|77-getter|0|77-preInit
    /**
     * Returns an initiliazed instance of exitCommand2 component.
     * @return the initialized component instance
     */
    public Command getExitCommand2() {
        if (exitCommand2 == null) {//GEN-END:|77-getter|0|77-preInit
            // write pre-init user code here
            exitCommand2 = new Command("Sair", Command.EXIT, 0);//GEN-LINE:|77-getter|1|77-postInit
            // write post-init user code here
        }//GEN-BEGIN:|77-getter|2|
        return exitCommand2;
    }
    //</editor-fold>//GEN-END:|77-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand ">//GEN-BEGIN:|81-getter|0|81-preInit
    /**
     * Returns an initiliazed instance of backCommand component.
     * @return the initialized component instance
     */
    public Command getBackCommand() {
        if (backCommand == null) {//GEN-END:|81-getter|0|81-preInit
            // write pre-init user code here
            backCommand = new Command("Voltar", Command.BACK, 0);//GEN-LINE:|81-getter|1|81-postInit
            // write post-init user code here
        }//GEN-BEGIN:|81-getter|2|
        return backCommand;
    }
    //</editor-fold>//GEN-END:|81-getter|2|



    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: splashScreen ">//GEN-BEGIN:|89-getter|0|89-preInit
    /**
     * Returns an initiliazed instance of splashScreen component.
     * @return the initialized component instance
     */
    public SplashScreen getSplashScreen() {
        if (splashScreen == null) {//GEN-END:|89-getter|0|89-preInit
            // write pre-init user code here
            splashScreen = new SplashScreen(getDisplay());//GEN-BEGIN:|89-getter|1|89-postInit
            splashScreen.setTitle("splashScreen");
            splashScreen.setCommandListener(this);
            splashScreen.setFullScreenMode(true);
            splashScreen.setImage(getImage1());
            splashScreen.setText("Frostah\'s Dice Roller");
            splashScreen.setTextFont(getFont1());
            splashScreen.setTimeout(3000);//GEN-END:|89-getter|1|89-postInit
            // write post-init user code here
        }//GEN-BEGIN:|89-getter|2|
        return splashScreen;
    }
    //</editor-fold>//GEN-END:|89-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: font1 ">//GEN-BEGIN:|92-getter|0|92-preInit
    /**
     * Returns an initiliazed instance of font1 component.
     * @return the initialized component instance
     */
    public Font getFont1() {
        if (font1 == null) {//GEN-END:|92-getter|0|92-preInit
            // write pre-init user code here
            font1 = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_ITALIC, Font.SIZE_MEDIUM);//GEN-LINE:|92-getter|1|92-postInit
            // write post-init user code here
        }//GEN-BEGIN:|92-getter|2|
        return font1;
    }
    //</editor-fold>//GEN-END:|92-getter|2|



    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay () {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable (null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet ();
        } else {
            initialize ();
            startMIDlet ();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }

}
